﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TV_2013.Handle;
using TV_2013.Model;

namespace TV_2013.View.MiniGame
{
    public partial class MiniGame1View : PhoneApplicationPage
    {
        ReadData readData = new ReadData("Data/DataMiniGame.xml");
        MiniGameModel miniGame = new MiniGameModel();
        private string key = "";
        private string id;
        private ChooseFile choose;
        
        
        public MiniGame1View()
        {
            InitializeComponent();
            choose = new ChooseFile();
        }

     

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            id = NavigationContext.QueryString["id"];
            miniGame = readData.GetMiniGameDataByAttribute(id, 4); // id cua lesson va so luong sound de load
            img1.Source = new BitmapImage(new Uri(miniGame.ArrayImage[0], UriKind.Relative));
            img2.Source = new BitmapImage(new Uri(miniGame.ArrayImage[1], UriKind.Relative));
            img3.Source = new BitmapImage(new Uri(miniGame.ArrayImage[2], UriKind.Relative));
            img4.Source = new BitmapImage(new Uri(miniGame.ArrayImage[3], UriKind.Relative));
            txt1.Text = miniGame.ArrayText[0];
            txt2.Text = miniGame.ArrayText[1];
            txt3.Text = miniGame.ArrayText[2];
            txt4.Text = miniGame.ArrayText[3];
          //  soundLesson.Source = new Uri(miniGame.SoundUrl, UriKind.Relative);
         
        }

        private void img1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            key = "A";
            if (key == miniGame.Key)
            {

                id = (Int32.Parse(id) + 1).ToString();
              
                if (Int32.Parse(id)>24)
                {
                    soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                    soundLesson.Play();
                    
                    MessageBox.Show("Bạn đã hoàn thành hết các màn chơi. Xin chúc mừng bạn");
                }

                else
                {
                    miniGame = readData.GetMiniGameDataByAttribute(id, 4); // id cua lesson va so luong sound de load
                    img1.Source = new BitmapImage(new Uri(miniGame.ArrayImage[0], UriKind.Relative));
                    img2.Source = new BitmapImage(new Uri(miniGame.ArrayImage[1], UriKind.Relative));
                    img3.Source = new BitmapImage(new Uri(miniGame.ArrayImage[2], UriKind.Relative));
                    img4.Source = new BitmapImage(new Uri(miniGame.ArrayImage[3], UriKind.Relative));
                    txt1.Text = miniGame.ArrayText[0];
                    txt2.Text = miniGame.ArrayText[1];
                    txt3.Text = miniGame.ArrayText[2];
                    txt4.Text = miniGame.ArrayText[3];
                    soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                }
                
            }

            else
            {
                soundLesson.Source = new Uri(choose.getBadFile(), UriKind.Relative);
                soundLesson.Play();
            }
        }

        private void img2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            key = "B";
            if (key == miniGame.Key)
            {

                id = (Int32.Parse(id) + 1).ToString();

                if (Int32.Parse(id) > 24)
                {
                    soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                    soundLesson.Play();
                    MessageBox.Show("Bạn đã hoàn thành hết các màn chơi. Xin chúc mừng bạn");
                }

                else
                {
                    miniGame = readData.GetMiniGameDataByAttribute(id, 4); // id cua lesson va so luong sound de load
                    img1.Source = new BitmapImage(new Uri(miniGame.ArrayImage[0], UriKind.Relative));
                    img2.Source = new BitmapImage(new Uri(miniGame.ArrayImage[1], UriKind.Relative));
                    img3.Source = new BitmapImage(new Uri(miniGame.ArrayImage[2], UriKind.Relative));
                    img4.Source = new BitmapImage(new Uri(miniGame.ArrayImage[3], UriKind.Relative));
                    txt1.Text = miniGame.ArrayText[0];
                    txt2.Text = miniGame.ArrayText[1];
                    txt3.Text = miniGame.ArrayText[2];
                    txt4.Text = miniGame.ArrayText[3];
                    soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                }

            }

            else
            {
                soundLesson.Source = new Uri(choose.getBadFile(), UriKind.Relative);
                soundLesson.Play();
            }
        }

        private void img3_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            key = "C";
            if (key == miniGame.Key)
            {

                id = (Int32.Parse(id) + 1).ToString();

                if (Int32.Parse(id) > 24)
                {
                    soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                    soundLesson.Play();
                    MessageBox.Show("Bạn đã hoàn thành hết các màn chơi. Xin chúc mừng bạn");
                }

                else
                {
                    miniGame = readData.GetMiniGameDataByAttribute(id, 4); // id cua lesson va so luong sound de load
                    img1.Source = new BitmapImage(new Uri(miniGame.ArrayImage[0], UriKind.Relative));
                    img2.Source = new BitmapImage(new Uri(miniGame.ArrayImage[1], UriKind.Relative));
                    img3.Source = new BitmapImage(new Uri(miniGame.ArrayImage[2], UriKind.Relative));
                    img4.Source = new BitmapImage(new Uri(miniGame.ArrayImage[3], UriKind.Relative));
                    txt1.Text = miniGame.ArrayText[0];
                    txt2.Text = miniGame.ArrayText[1];
                    txt3.Text = miniGame.ArrayText[2];
                    txt4.Text = miniGame.ArrayText[3];
                    soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                }

            }

            else
            {
                soundLesson.Source = new Uri(choose.getBadFile(), UriKind.Relative);
                soundLesson.Play();
            }
        }

        private void img4_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            key = "D";
            if (key == miniGame.Key)
            {


                id = (Int32.Parse(id) + 1).ToString();

                if (Int32.Parse(id) > 24)
                {
                    soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                    soundLesson.Play();
                    MessageBox.Show("Bạn đã hoàn thành hết các màn chơi. Xin chúc mừng bạn");
                }

                else
                {
                    miniGame = readData.GetMiniGameDataByAttribute(id, 4); // id cua lesson va so luong sound de load
                    img1.Source = new BitmapImage(new Uri(miniGame.ArrayImage[0], UriKind.Relative));
                    img2.Source = new BitmapImage(new Uri(miniGame.ArrayImage[1], UriKind.Relative));
                    img3.Source = new BitmapImage(new Uri(miniGame.ArrayImage[2], UriKind.Relative));
                    img4.Source = new BitmapImage(new Uri(miniGame.ArrayImage[3], UriKind.Relative));
                    txt1.Text = miniGame.ArrayText[0];
                    txt2.Text = miniGame.ArrayText[1];
                    txt3.Text = miniGame.ArrayText[2];
                    txt4.Text = miniGame.ArrayText[3];
                    soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                }

            }

            else
            {
                soundLesson.Source = new Uri(choose.getBadFile(), UriKind.Relative);
                soundLesson.Play();
            }
        }

        private void btn_play_Click(object sender, RoutedEventArgs e)
        {
            soundLesson.Source = new Uri(miniGame.SoundUrl, UriKind.Relative);
        }        
    }
}